#include "Path.h"

#define PATH_INITIALLEN		256

//#define PATH_OPTIMIZATION

CPath::CPath(CMap *map)
{
	m_map = map;
	m_path = new TPathPoint[PATH_INITIALLEN];
	m_pathMaxLen = PATH_INITIALLEN;

	m_pathLen = 0;
	m_pathPos = 0;
}

void CPath::reset()
{
	m_pathLen = 0;
	m_pathPos = 0;
}

int CPath::addPoint(int x, int y, bool endPoint)
{
	bool addNewPoint = true;

#ifdef PATH_OPTIMIZATION
	if ((m_pathLen > 1)&&(!endPoint))
	{
		if (m_map->canDrawLine(m_path[m_pathLen-2].x, m_path[m_pathLen-2].y, x, y))
		{
			m_path[m_pathLen-1].x = x;
			m_path[m_pathLen-1].y = y;
			addNewPoint = false;
		}
	}
#endif

	if (addNewPoint)
	{		
		m_path[m_pathLen].x = x;
		m_path[m_pathLen].y = y;
		m_pathLen++;

		if (m_pathLen>=m_pathMaxLen)
		{
			int nl = (m_pathMaxLen*3)/2;
			TPathPoint *tmp = new TPathPoint[nl];
			for (int i=0;i<m_pathMaxLen;i++)
				tmp[i] = m_path[i];
			m_pathMaxLen = nl;
			delete[] m_path;
			m_path = tmp;
		}
	}

	return m_pathLen-1;
}

int CPath::getSize()
{
	return m_pathLen;
}

int CPath::optimize()
{
	return 0;
}

bool CPath::hasMorePoints()
{
	return (m_pathPos<m_pathLen);
}

CPath::TPathPoint CPath::getNextPoint()
{
	if (m_pathPos<m_pathLen)
		return m_path[m_pathPos++];
	else
		return m_path[m_pathLen-1];
}

CPath::TPathPoint CPath::getPoint(int index)
{
	if (index<m_pathLen)
		return m_path[index];

	return m_path[m_pathLen-1];
}
